package forza4;

import java.io.FileNotFoundException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class FrmImpostazioni {

	protected Object result;
	protected Shell shell;
	private Text txtFileProlog;
	private Text txtFileAlbero;
	private String pathFileProlog;
	private String pathFileAlbero;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public FrmImpostazioni() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(FrmImpostazioni.class, "/img/icona.png"));
		shell.setSize(487, 144);
		shell.setText("Impostazioni");
		
		Label lblPercorsoFileProlog = new Label(shell, SWT.NONE);
		lblPercorsoFileProlog.setBounds(10, 22, 113, 15);
		lblPercorsoFileProlog.setText("Percorso File Prolog");
		
		txtFileProlog = new Text(shell, SWT.BORDER);
		txtFileProlog.setBounds(129, 19, 305, 21);
		txtFileProlog.setText(Impostazioni.pathFileProlog);
		
		txtFileAlbero = new Text(shell, SWT.BORDER);
		txtFileAlbero.setBounds(129, 45, 305, 21);
		txtFileAlbero.setText(Impostazioni.pathFileAlbero);
		
		Button btnFileAlbero = new Button(shell, SWT.NONE);
		btnFileAlbero.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				DirectoryDialog dlg = new DirectoryDialog(shell);
			    pathFileAlbero = dlg.open();
			    txtFileAlbero.setText(pathFileAlbero);
			}
		});
		btnFileAlbero.setBounds(440, 43, 21, 25);
		btnFileAlbero.setText("...");
		
		Button btnApriFileProlog = new Button(shell, SWT.NONE);
		btnApriFileProlog.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				DirectoryDialog dlg = new DirectoryDialog(shell);
			    pathFileProlog = dlg.open();
			    txtFileProlog.setText(pathFileProlog);
			}
		});
		btnApriFileProlog.setBounds(440, 17, 21, 25);
		btnApriFileProlog.setText("...");
		
		Label lblPercorsoFileAlbero = new Label(shell, SWT.NONE);
		lblPercorsoFileAlbero.setBounds(10, 48, 113, 15);
		lblPercorsoFileAlbero.setText("Percorso File Albero");
		
		Button btnOK = new Button(shell, SWT.NONE);
		btnOK.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Impostazioni.pathFileAlbero=txtFileAlbero.getText();
				Impostazioni.pathFileProlog=txtFileProlog.getText();
				try {
					Impostazioni.salvaImpostazioniToXML();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					MessageBox message = new MessageBox(shell,SWT.ICON_INFORMATION);
					message.setText("Impossibile salvare il file delle impostazioni");
					message.open();
				}
				shell.dispose();
			}
		});
		btnOK.setBounds(108, 72, 75, 25);
		btnOK.setText("OK");
		
		Button btnAnnulla = new Button(shell, SWT.NONE);
		btnAnnulla.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.dispose();
			}
		});
		btnAnnulla.setBounds(291, 72, 75, 25);
		btnAnnulla.setText("Annulla");
		
		shell.open();
		shell.layout();
		Display display = shell.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}
